home *** CD-ROM | disk | FTP | other *** search
-
- {*******************************************************}
- { }
- { Delphi Visual Component Library }
- { }
- { Copyright (c) 1996 Borland International }
- { }
- {*******************************************************}
-
- unit OleAuto;
-
- {$R-}
-
- interface
-
- uses Windows, Ole2, OleCtl, SysUtils;
-
- const
-
- { Maximum number of dispatch arguments }
-
- MaxDispArgs = 32;
-
- type
-
- { Forward declarations }
-
- TAutoObject = class;
-
- { Dispatch interface for TAutoObject }
-
- TAutoDispatch = class(IDispatch)
- public
- constructor Create(AutoObject: TAutoObject);
- function QueryInterface(const iid: TIID; var obj): HResult; override;
- function AddRef: Longint; override;
- function Release: Longint; override;
- function GetTypeInfoCount(var ctinfo: Integer): HResult; override;
- function GetTypeInfo(itinfo: Integer; lcid: TLCID;
- var tinfo: ITypeInfo): HResult; override;
- function GetIDsOfNames(const iid: TIID; rgszNames: POleStrList;
- cNames: Integer; lcid: TLCID; rgdispid: PDispIDList): HResult; override;
- function Invoke(dispIDMember: TDispID; const iid: TIID; lcid: TLCID;
- flags: Word; var dispParams: TDispParams; varResult: PVariant;
- excepInfo: PExcepInfo; argErr: PInteger): HResult; override;
- function GetAutoObject: TAutoObject; virtual; stdcall;
- property AutoObject: TAutoObject;
- end;
-
- { TAutoObject - Automation object base class. An automation class is
- implemented by deriving a new class from TAutoObject, and declaring methods
- and properties in an "automated" section in the new class. To expose an
- automation class to external OLE Automation Controllers, the unit that
- implements the automation class must call Automation.RegisterClass in its
- initialization section, passing in a TAutoClassInfo structure. Once a
- class has been registered in this way, the global Automation object
- automatically manages all aspects of interfacing with the OLE Automation
- APIs.
-
- When an external OLE Automation Controller requests an instance of an
- automation class, the Create constructor is called to create the object,
- and when all external references to the object disappear, the Destroy
- destructor is called to destroy the object. As is the case with all OLE
- objects, automation objects are reference counted. }
-
- TAutoObject = class(TObject)
- protected
- function CreateAutoDispatch: TAutoDispatch; virtual;
- procedure GetExceptionInfo(ExceptObject: TObject;
- var ExcepInfo: TExcepInfo); virtual;
- public
- constructor Create; virtual;
- destructor Destroy; override;
- function AddRef: Integer;
- function Release: Integer;
- property AutoDispatch: TAutoDispatch;
- property OleObject: Variant;
- property RefCount: Integer;
- end;
-
- { Automation object class reference }
-
- TAutoClass = class of TAutoObject;
-
- { Instancing mode for local server automation classes }
-
- TAutoClassInstancing = (acInternal, acSingleInstance, acMultiInstance);
-
- { Automation class registration info }
-
- TAutoClassInfo = record
- AutoClass: TAutoClass;
- ProgID: string;
- ClassID: string;
- Description: string;
- Instancing: TAutoClassInstancing;
- end;
-
- { Class registry entry }
-
- TRegistryClass = class
- public
- constructor Create(const AutoClassInfo: TAutoClassInfo);
- destructor Destroy; override;
- procedure UpdateRegistry(Register: Boolean);
- end;
-
- { Application start mode }
-
- TStartMode = (smStandalone, smAutomation, smRegServer, smUnregServer);
-
- { Automation manager event types }
-
- TLastReleaseEvent = procedure(var Shutdown: Boolean) of object;
-
- { Automation manager object }
-
- TAutomation = class
- public
- constructor Create;
- destructor Destroy; override;
- procedure RegisterClass(const AutoClassInfo: TAutoClassInfo);
- procedure UpdateRegistry(Register: Boolean);
- property AutoObjectCount: Integer;
- property IsInprocServer: Boolean;
- property StartMode: TStartMode;
- property OnLastRelease: TLastReleaseEvent;
- end;
-
- { OLE exception classes }
-
- EOleError = class(Exception);
-
- EOleSysError = class(EOleError)
- public
- constructor Create(ErrorCode: Integer);
- property ErrorCode: Integer;
- end;
-
- EOleException = class(EOleError)
- public
- constructor Create(const ExcepInfo: TExcepInfo);
- property ErrorCode: Integer;
- property HelpFile: string;
- property Source: string;
- end;
-
- { Dispatch call descriptor }
-
- PCallDesc = ^TCallDesc;
- TCallDesc = packed record
- CallType: Byte;
- ArgCount: Byte;
- NamedArgCount: Byte;
- ArgTypes: array[0..255] of Byte;
- end;
-
- var
- Automation: TAutomation;
-
- { CreateOleObject creates an OLE automation object of the given class. }
-
- function CreateOleObject(const ClassName: string): Variant;
-
- { GetActiveOleObject returns the active object for the given class. }
-
- function GetActiveOleObject(const ClassName: string): Variant;
-
- { The DllXXXX routines implement the required entry points of an in-process
- automation server DLL. These routines must be exported by the DLL using
- an "exports" clause in the library's main module. }
-
- function DllGetClassObject(const CLSID: TCLSID; const IID: TIID;
- var Obj): HResult; stdcall;
- function DllCanUnloadNow: HResult; stdcall;
- function DllRegisterServer: HResult; stdcall;
- function DllUnregisterServer: HResult; stdcall;
-
- { VarFromInterface returns a variant that contains the a reference to the
- IDispatch interface of the given IUnknown interface. If the Unknown
- parameter is NIL, the resulting variant is set to Unassigned. }
-
- function VarFromInterface(Unknown: IUnknown): Variant;
-
- { VarToInterface returns the IDispatch interface reference stored in the
- given variant. An exception is raised if the variant does not contain
- an IDispatch interface. VarToInterface does not affect the reference
- count of the returned IDispatch. The caller of VarToInterface must
- manually call AddRef and Release on the returned interface. }
-
- function VarToInterface(const V: Variant): IDispatch;
-
- { VarToAutoObject returns the TAutoObject instance corresponding to the
- IDispatch interface reference stored in the given variant. An exception
- is raised if the variant does not contain an IDispatch interface, or if
- the IDispatch interface is not that of a TAutoObject instance. }
-
- function VarToAutoObject(const V: Variant): TAutoObject;
-
- procedure DispInvoke(Dispatch: IDispatch; CallDesc: PCallDesc;
- DispIDs: PDispIDList; Params: Pointer; Result: PVariant);
- procedure DispInvokeError(Status: Integer; const ExcepInfo: TExcepInfo);
-
- procedure OleError(ErrorCode: HResult);
- procedure OleCheck(Result: HResult);
-
- function StringToClassID(const S: string): TCLSID;
- function ClassIDToString(const ClassID: TCLSID): string;
-
- function ProgIDToClassID(const ProgID: string): TCLSID;
- function ClassIDToProgID(const ClassID: TCLSID): string;
-
- implementation
-